import LoopScrollView from "../Util/LoopScrollView";
import CCCompentHelper from "../Util/CCCompentHelper";
import XRole from "../../XRole";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ExampleTest extends cc.Component {

    @property({ tooltip: "循环列表" } || cc.Boolean)
    @property({ type: LoopScrollView, serializable: true, displayName: "循环列表" })
    mScrollView: LoopScrollView = null;

    mCurWPItemNums = 0;

    //多行多列所需
    @property(cc.Prefab)
    mMoreRowPrefab: cc.Prefab = null;
    mMoreRowPrefabScript: string = "ExampleLayout";

    //单行单列
    @property(cc.Prefab)
    mOneRowPrefab: cc.Prefab = null;
    mOneRowPrefabScript: string = "ExampleLayout1";

    mCurScrollViewType = 0;

    onLoad() {
        CCCompentHelper.RigisterButtonObjectEvent(this.node, "addItemBtn", () => {
            XRole.instance().mItemBaseMap.set(this.mCurWPItemNums.toString(), null);
            let curNums = this.mCurWPItemNums += 1;
            this.mScrollView.RefreshItem(curNums);
        });

        CCCompentHelper.RigisterButtonObjectEvent(this.node, "delItemBtn", () => {
            if (XRole.instance().mItemBaseMap.size <= 0) {
                return;
            }
            this.mCurWPItemNums = Math.max(0, this.mCurWPItemNums -= 1);
            XRole.instance().mItemBaseMap.delete(this.mCurWPItemNums.toString());
            this.mScrollView.RefreshItem(this.mCurWPItemNums);
        });

        CCCompentHelper.RigisterButtonObjectEvent(this.node, "changeBtn", () => {
            XRole.instance().mItemBaseMap.clear();
            this.mCurWPItemNums = 0;
            this.mScrollView.RefreshItem(this.mCurWPItemNums);
            this.mCurScrollViewType = 1 - this.mCurScrollViewType;
            //切换Loop数据
            if (this.mCurScrollViewType == 1) {
                this.mScrollView.itemScriptName = this.mOneRowPrefabScript;
                this.mScrollView.itemPrefab = this.mOneRowPrefab;
            } else {
                this.mScrollView.itemScriptName = this.mMoreRowPrefabScript;
                this.mScrollView.itemPrefab = this.mMoreRowPrefab;
            }
            this.mScrollView.items = [];
        });
    }
}
